package com.pj.point.receiver;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.pj.point.Inform;
import com.pj.point.R;
import com.pj.point.litepal.PointJavaBean;
import com.pj.point.point.NewPoint;

import org.litepal.LitePal;

import static android.content.Context.NOTIFICATION_SERVICE;

public class AlertReceiver extends BroadcastReceiver {

    private NotificationManager manager;
    private Notification notification;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onReceive(Context context, Intent intent) {
        String title = intent.getStringExtra("title");
        String content = intent.getStringExtra("content");
        String pointId = intent.getStringExtra("pointId");
        PointJavaBean bean = LitePal.where("pointId=?", pointId).findFirst(PointJavaBean.class);
        bean.setState(2);
        bean.save();
        manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Intent myIntent = new Intent(context, Inform.class);
        myIntent.putExtra("title",title);
        myIntent.putExtra("content",content);
        PendingIntent pendingIntent = PendingIntent.getActivities(context, 3, new Intent[]{myIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
        notification = new NotificationCompat.Builder(context, "msgId")
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(R.mipmap.app_icon)
                .setColor(Color.parseColor("#000000"))
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.lover))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
        NotificationChannel channel = new NotificationChannel("msgId", "来自point的通知", NotificationManager.IMPORTANCE_HIGH);
        manager.createNotificationChannel(channel);
        manager.notify(1,notification);
    }

}